package org.jactao.rpc;

import org.jactao.common.LRequest;
import org.jactao.remote.netty.NettyClient;

import java.net.InetSocketAddress;
import java.util.Objects;

/**
 * Created by fanzetao on 2018/4/4.
 */
public abstract class LChannel {

    private int weight = 0;

    protected InetSocketAddress address;

    protected String serializer;

    public InetSocketAddress getAddress() {
        return address;
    }

    public void setAddress(InetSocketAddress address) {
        this.address = address;
    }

    public void setWeight(int weight) {
        this.weight = weight;
    }

    public int getWeight() {
        return weight;
    }

    public void setSerializer(String serializer) {
        this.serializer = serializer;
    }

    public String getSerializer() {
        return serializer;
    }

    public abstract void send(LRequest request);

    public abstract void connect();

    public abstract boolean connected();

    public static LChannel get(String protocolName) {
        switch (protocolName) {
            case "netty":
                return new NettyClient();
            case "grizzly":
                return null;
            default:
                return null;
        }
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        LChannel lChannel = (LChannel) o;
        return Objects.equals(address.getHostName() + address.getPort(),
                lChannel.address.getHostName() + lChannel.getAddress().getPort());
    }

    @Override
    public int hashCode() {
        return Objects.hash(address.getHostName() + address.getPort());
    }
}
